/**
 * 列表数据获取
 * @param {Function} listRequest 请求方法
 * @param {Object} data 请求参数
 * @param {any}ref_el ref element
 */
export default function useList(listRequest, data, ref_el) {
	const $el = ref_el; /// ref element;

	const loading = ref(false); /// loading state;
	const currentPage = ref(1); /// current page;

	const total = ref(0); /// total count;
	const pageSize = ref(10); /// page size;

	const list = ref([]); /// list data;

	/**
	 * 数据加载
	 */
	const loadData = async () => {
		loading.value = true;
		try {
			const resultParams = Object.assign({}, { page: currentPage.value, pageSize: pageSize.value }, data);
			const {
				data: { list, total: totals }
			} = await listRequest(resultParams);
			// set list data
			list.value = list;
			// set total count
			total.value = totals;
		} catch (error) {
			console.log(error, "请求错误");
		} finally {
			// close loading state
			loading.value = false;
		}
	};

	// watch currentPage change
	watch(
		[currentPage, pageSize],
		() => {
			loadData();
		},
		{ immediate: true }
	);
	return {
		$el,
		loading,
		currentPage,
		total,
		pageSize,
		list,
		loadData
	};
}
